package com.fpx.order.plat.common.utils;

import java.io.InputStream;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

/**
 * desc: 单例模式创建缓存实例
 * @author xiangtao(xiangtao@4px.com)
 * @date 2015年11月26日 下午6:33:10
 * @version 1.0
 */
public class CacheInstance {

	private static final String S_EHCACHE_FILE = "cfg/ehcache/ehcache.xml"; 
	private CacheInstance()
	{
		
	}
	

	
	
	 private static class CacheInstanceHolder {
	        private final static Cache INSTANCE = getCache();
	 }
	
	 public static Cache getInstatnce(){
		 return CacheInstanceHolder.INSTANCE;
	 }

	
	
	
	
	/**
	 * 创建缓存管理对象  
	 * 并将配置文件 条件属性设置到对象
	 * @return
	 */
	private static CacheManager getCacheManager() {
		InputStream objIS = CacheInstance.class.getResourceAsStream(S_EHCACHE_FILE);
		CacheManager objManager=new CacheManager(objIS);
		return objManager;
	}
	
	/**
	 *  获取缓存对象 
	 * @return
	 */
	private static Cache getCache() {
		CacheManager objManager=getCacheManager();
		if(objManager.cacheExists("basiccache"))
		{
			return objManager.getCache("basiccache");
		}
		else
		{
			Cache objCache=new Cache("basiccache",10000,true,false,120000,120000);
			objManager.addCache(objCache);
			
			return objCache;
		}
	}
}
